home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / DOSENV.C < prev    next >
C/C++ Source or Header  |  1992-05-05  |  6KB  |  218 lines

  1. /* -*-C-*-
  2.  
  3. $Header: /scheme/dos386/microcode/RCS/dosenv.c,v 1.1 1992/05/05 06:55:13 jinx Exp $
  4.  
  5. Copyright (c) 1992 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. #include "msdos.h"
  36. #include "osenv.h"
  37. #include <stdlib.h>
  38.  
  39. void
  40. DEFUN (OS_current_time, (buffer), struct time_structure * buffer)
  41. {
  42.   time_t t;
  43.   struct tm * ts;
  44.   STD_UINT_SYSTEM_CALL (syscall_time, t, (DOS_time (0)));
  45.   STD_PTR_SYSTEM_CALL (syscall_localtime, ts, (DOS_localtime (&t)));
  46.   (buffer -> year) = ((ts -> tm_year) + 1900);
  47.   (buffer -> month) = ((ts -> tm_mon) + 1);
  48.   (buffer -> day) = (ts -> tm_mday);
  49.   (buffer -> hour) = (ts -> tm_hour);
  50.   (buffer -> minute) = (ts -> tm_min);
  51.   (buffer -> second) = (ts -> tm_sec);
  52.   {
  53.     /* In localtime() encoding, 0 is Sunday; in ours, it's Monday. */
  54.     int wday = (ts -> tm_wday);
  55.     (buffer -> day_of_week) = ((wday == 0) ? 6 : (wday - 1));
  56.   }
  57. }
  58.  
  59. clock_t
  60. DEFUN_VOID (OS_process_clock)
  61. {
  62.   /* This must not signal an error in normal use. */
  63.   /* Return answer in milliseconds, was in 1/100th seconds */
  64.   return (clock()*((clock_t) (1000/CLOCKS_PER_SEC)));
  65. }
  66.  
  67.  
  68.  
  69. clock_t
  70. DEFUN_VOID (OS_real_time_clock)
  71. {
  72.   return (clock()*((clock_t) (1000/CLOCKS_PER_SEC)));
  73. }
  74.  
  75.  
  76.  
  77. /* Timer adjustments */
  78. #define PC_TIMER_TICKS_PER_SECOND    (18.2)
  79. /* This should work out to about 55 */
  80. #define PC_MILLISECONDS_PER_TIMER_TICK  \
  81.   ((long) ((1000.0/PC_TIMER_TICKS_PER_SECOND)+0.5))
  82.  
  83. static unsigned long
  84. DEFUN (ms_to_ticks, (clocks), clock_t clocks)
  85. { ldiv_t ticks;
  86.   unsigned long result;
  87.  
  88.   ticks = ldiv((long) clocks, PC_MILLISECONDS_PER_TIMER_TICK);
  89.  
  90.   result = ((ticks.rem >= (PC_MILLISECONDS_PER_TIMER_TICK/2)) ?
  91.            (ticks.quot + 1) : (ticks.quot));
  92.   return (result == 0) ? 1 : result;  
  93. }
  94.   
  95. void
  96. DEFUN (OS_process_timer_set, (first, interval),
  97.        clock_t first AND
  98.        clock_t interval)
  99. { extern volatile unsigned long scm_itimer_counter, scm_itimer_reload;
  100.   /* Convert granularity to 1/18.2 seconds */
  101.  
  102.   scm_itimer_counter = ms_to_ticks(first);
  103.   scm_itimer_reload  = ms_to_ticks(interval);
  104.   
  105.   return;  
  106. }
  107.  
  108. void
  109. DEFUN_VOID (OS_process_timer_clear)
  110. {
  111.   scm_itimer_reload = scm_itimer_counter = 0;
  112.   return;
  113. }
  114.  
  115. void
  116. DEFUN (OS_real_timer_set, (first, interval),
  117.        clock_t first AND
  118.        clock_t interval)
  119. {
  120.   OS_process_timer_set (first, interval);
  121. }
  122.  
  123. void
  124. DEFUN_VOID (OS_real_timer_clear)
  125. {
  126.   OS_process_timer_clear();
  127.   return;
  128. }
  129.  
  130. void
  131. DEFUN_VOID (DOS_initialize_environment)
  132. {
  133.   return;
  134. }
  135.  
  136. static size_t current_dir_path_size = 0;
  137. static char * current_dir_path = 0;
  138.  
  139. CONST char *
  140. DEFUN_VOID (OS_working_dir_pathname)
  141. {
  142.   if (current_dir_path) {
  143.     return (current_dir_path);
  144.   }
  145.   if (current_dir_path_size == 0)
  146.     {
  147.       current_dir_path = (DOS_malloc (1024));
  148.       if (current_dir_path == 0)
  149.     error_system_call (ENOMEM, syscall_malloc);
  150.       current_dir_path_size = 1024;
  151.     }
  152.   while (1)
  153.     {
  154.       if ((DOS_getcwd (current_dir_path, current_dir_path_size)) != 0)
  155.       { strlwr(current_dir_path);
  156.     return (current_dir_path);
  157.       }
  158. #ifdef ERANGE
  159.       if (errno != ERANGE)
  160.     error_system_call (errno, syscall_getcwd);
  161. #endif      
  162.       current_dir_path_size *= 2;
  163.       {
  164.     char * new_current_dir_path =
  165.       (DOS_realloc (current_dir_path, current_dir_path_size));
  166.     if (new_current_dir_path == 0)
  167.       /* ANSI C requires `path' to be unchanged -- we may have to
  168.          discard it for systems that don't behave thus. */
  169.       error_system_call (ENOMEM, syscall_realloc);
  170.     current_dir_path = new_current_dir_path;
  171.       }
  172.     }
  173. }
  174.  
  175. void
  176. DEFUN (OS_set_working_dir_pathname, (name), char * name)
  177. { char filename[128], drive[3];
  178.   int drive_number;
  179.   size_t name_size = strlen (name);
  180.   
  181.   drive_number = dos_split_filename(name, drive, filename);
  182.   dos_set_default_drive(drive_number);
  183.   STD_VOID_SYSTEM_CALL (syscall_chdir, (DOS_chdir (filename)));
  184.  
  185.   while (1) {
  186.     if (name_size < current_dir_path_size) {
  187.       strcpy(current_dir_path, name);
  188.       return;
  189.     } 
  190.     current_dir_path_size *= 2;
  191.     {
  192.       char * new_current_dir_path =
  193.     (DOS_realloc (current_dir_path, current_dir_path_size));
  194.       if (new_current_dir_path == 0)
  195.     error_system_call (ENOMEM, syscall_realloc);
  196.       current_dir_path = new_current_dir_path;
  197.     }
  198.   }
  199. }
  200.  
  201. CONST char *
  202. DEFUN (OS_get_environment_variable, (name), CONST char * name)
  203. {
  204.   return (DOS_getenv (name));
  205. }
  206.  
  207. CONST char *
  208. DEFUN_VOID (OS_current_user_name)
  209. {
  210.   return ("dos");
  211. }
  212.  
  213. CONST char *
  214. DEFUN_VOID (OS_current_user_home_directory)
  215. {
  216.   return ("c:\\");
  217. }
  218.